<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            color: red;
        }
    </style>
</head>

<body>
    <div id="root"></div>
    <!-- 引入依赖 -->
    <script src="../lib/react.js"></script>
    <script src="../lib/react-dom.js"></script>
    <script src="../lib/babel.js"></script>
    <script type="text/babel">
        /* 点击电影，电影字体变为红色 */
        class App extends React.Component {
            constructor() {
                super()
                this.state = {
                    message: "电影列表",
                    movies: ["黑色星期五", "魔山", "电锯惊魂", "山村老尸", "安娜贝尔"],
                    currentIndex: 0
                }
                this.selectMov = this.selectMov.bind(this)
            }

            selectMov(index) {
                this.setState({
                    currentIndex:index
                })
            }

            render() {
                const { message, movies, currentIndex } = this.state
                return (
                    <div>
                        <h2>{message}</h2>
                        <ul>
                            {movies.map((item, index) => <li key={item} className={currentIndex == index ? "active" : ''} onClick={() => { this.selectMov(index) }}>{item}</li>)}
                        </ul>
                    </div>
                )
            }
        }
        const root = ReactDOM.createRoot(document.querySelector("#root"))
        root.render(<App />)
    </script>
</body>

</html>